﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Drawing;

namespace AI3SaveEditor.AI3Files
{
    public abstract class GirlProfile : AI3File
    {
        const int NameLength = 64;

        protected string firstName;
        protected string lastName;
        PersonalityType personality;
        Color eyebrowColor;

        public GirlProfile(string fileName) : base(fileName) { }

        [Category("人物"), DisplayName("姓名-名"), Description("First Name")]
        public virtual string FirstName {
            get {
                return firstName;
            }
            set {
                this.firstName = value;
                IllusionUtils.SetString(value, buffer, Postions[GirlProfileProperties.FirstName], NameLength);
            }
        }

        [Category("人物"), DisplayName("姓名-姓"), Description("Last Name")]
        public virtual string LastName {
            get {
                return lastName;
            }
            set {
                this.lastName = value;
                IllusionUtils.SetString(value, buffer, Postions[GirlProfileProperties.LastName], NameLength);
            }
        }

        [Category("人物"), DisplayName("性格"), Description("人物性格")]
        public virtual PersonalityType Personality {
            get {
                return personality;
            }
            set {
                personality = value;
                SetInt32ToBuffer((int)value, Postions[GirlProfileProperties.Personality]);
            }
        }

        [Category("头部/脸部"), DisplayName("眉毛颜色"), Description("眉毛颜色")]
        public virtual Color EyebrowColor {
            get {
                return eyebrowColor;
            }
            set {
                eyebrowColor = value;
                buffer[Postions[GirlProfileProperties.EyebrowColor_R]] = value.R;
                buffer[Postions[GirlProfileProperties.EyebrowColor_G]] = value.G;
                buffer[Postions[GirlProfileProperties.EyebrowColor_B]] = value.B;
            }
        }

        internal abstract IDictionary<GirlProfileProperties, int> Postions {
            get;
        }

        public override void Load() {
            this.buffer = IllusionUtils.CMIDecrypt.DecryptFile(this.file.FullName);

            IllusionUtils.CMIDecrypt.DecryptString(this.buffer, Postions[GirlProfileProperties.LastName], NameLength);
            this.lastName = IllusionUtils.GetStringWithTrimNull(this.buffer, Postions[GirlProfileProperties.LastName], NameLength);

            IllusionUtils.CMIDecrypt.DecryptString(this.buffer, Postions[GirlProfileProperties.FirstName], NameLength);
            this.firstName = IllusionUtils.GetStringWithTrimNull(this.buffer, Postions[GirlProfileProperties.FirstName], NameLength);
            personality = (PersonalityType)BitConverter.ToInt32(buffer, Postions[GirlProfileProperties.Personality]);
            eyebrowColor = Color.FromArgb(
                buffer[Postions[GirlProfileProperties.EyebrowColor_R]],
                buffer[Postions[GirlProfileProperties.EyebrowColor_G]],
                buffer[Postions[GirlProfileProperties.EyebrowColor_B]]);
        }

        protected override void DecryptFileContent(byte[] fileContent) {
            IllusionUtils.CMIDecrypt.DecryptString(fileContent, Postions[GirlProfileProperties.LastName], NameLength);
            IllusionUtils.CMIDecrypt.DecryptString(fileContent, Postions[GirlProfileProperties.FirstName], NameLength);
            IllusionUtils.CMIDecrypt.DecryptBuffer(fileContent);
        }
    }

    internal enum GirlProfileProperties
    {
        FirstName,
        LastName,
        Personality,
        EyebrowColor_R,
        EyebrowColor_G,
        EyebrowColor_B,
    }
}
